import os
import string

import zipfile

import base64

from Proxy.Servers.ServersInterface import ServersInterface

proxyHost = "http-dynamic.xiaoxiangdaili.com"
proxyPort = "10030"
# 隧道身份信息
proxyUser = "694018019764359168"
proxyPass = "3qPrJVsO"
authStr = proxyUser + ":" + proxyPass

proxyAuth = "Basic " + base64.b64encode(authStr.encode('utf-8')).decode('utf-8')


class Abuyun(ServersInterface):
    """
    阿布云代理服务
    """
    def create_proxy_auth_extension(
            self,
            proxy_host: str,
            proxy_port: str,
            proxy_username: str,
            proxy_password: str,
            scheme: str = 'http',
            plugin_path: str = None
    ):
        """
        生成代理
        :param proxy_host:
        :param proxy_port:
        :param proxy_username:
        :param proxy_password:
        :param scheme:
        :param plugin_path:
        :return:
        """
        if plugin_path is None:
            plugin_path = r'./authProxy@http-dyn.abuyun.9020.zip'

        manifest_json = """
            {
                "version": "1.0.0",
                "manifest_version": 2,
                "name": "Abuyun Proxy",
                "permissions": [
                    "proxy",
                    "tabs",
                    "unlimitedStorage",
                    "storage",
                    "<all_urls>",
                    "webRequest",
                    "webRequestBlocking"
                ],
                "background": {
                    "scripts": ["background.js"]
                },
                "minimum_chrome_version":"22.0.0"
            }
            """

        background_js = string.Template(
            """
            var config = {
                mode: "fixed_servers",
                rules: {
                    singleProxy: {
                        scheme: "${scheme}",
                        host: "${host}",
                        port: parseInt(${port})
                    },
                    bypassList: ["foobar.com"]
                }
              };

            chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

            function callbackFn(details) {
                return {
                    authCredentials: {
                        username: "${username}",
                        password: "${password}"
                    }
                };
            }

            chrome.webRequest.onAuthRequired.addListener(
                callbackFn,
                {urls: ["<all_urls>"]},
                ['blocking']
            );
            """
        ).substitute(
            host=proxy_host,
            port=proxy_port,
            username=proxy_username,
            password=proxy_password,
            scheme=scheme,
        )

        with zipfile.ZipFile(plugin_path, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)

        return plugin_path

    def get_proxy_auth_plugin_path(self):
        """
        获取代理路径
        :return:
        """
        path = os.path.dirname(os.path.realpath(__file__))+os.path.sep
        return self.create_proxy_auth_extension(
            proxy_host=proxyHost,
            proxy_port=proxyPort,
            proxy_username=proxyUser,
            proxy_password=proxyPass,
            plugin_path=path+'authProxy@http-dyn.abuyun.9020.zip'
        )

#
# if __name__ == '__main__':
#     proxy = Proxy()
#     proxy_auth_plugin_path = proxy.get_proxy_auth_plugin_path()
#     print(proxy_auth_plugin_path)
